package com.example.iitematicviewer.api.machine;

import com.example.iitematicviewer.constant.Constant;
import com.example.iitematicviewer.constant.ExceptionConstant;
import com.example.iitematicviewer.mapper.MachineDao;
import com.example.iitematicviewer.vo.MachineVO;
import com.example.iitematicviewer.vo.ResultVO;
import net.sf.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 归档业务层
 */
@Service
public class MachineService {

    @Autowired
    private MachineTransService service;

    @Autowired
    MachineDao dao;

    /**
     * 查询单个
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO selectOne(JSONObject param,ResultVO resultVO){
        try {
            resultVO.setCode(MachineExceptionConstant.two);
            String id = param.getString(Constant.id);
            if (StringUtils.isNotEmpty(id)) {
                MachineVO machineVO = dao.selectOne(id);
                if (null != machineVO){
                    JSONObject jsonObject = JSONObject.parseObject(machineVO.toString());
                    resultVO.setData(jsonObject);
                }
            }
        }catch (Exception e){
            resultVO.setCode(MachineExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badSelect + e.getMessage());
        }
        return resultVO;
    }

    /**
     * 查询所有
     * @return
     */
    public ResultVO selectAll(ResultVO resultVO){
        try{
            resultVO.setCode(MachineExceptionConstant.two);
            List<MachineVO> machineServiceList = new ArrayList<>();
            machineServiceList = dao.selectAll();
            resultVO.setMessage(ExceptionConstant.select);
            resultVO.setData(transBySelectAll(machineServiceList));
        }catch (Exception e){
            resultVO.setCode(MachineExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badSelect + e.getMessage());
        }
        return resultVO;
    }

    /**
     * 将归档数据转换为JSON
     * @param machineServiceList
     * @return
     */
    public JSONObject transBySelectAll(List<MachineVO> machineServiceList){
        JSONObject obj = new JSONObject();
        JSONArray array = JSONArray.fromObject(machineServiceList);
        obj.put("machineList",array);
        return obj;
    }

    /**
     * 新增
     * @param param
     * @return
     */
    public ResultVO insert(JSONObject param,ResultVO resultVO){
        try{
            resultVO.setCode(MachineExceptionConstant.two);
            MachineVO machineVO = service.transJSONObjectByMachine(param);
            if (null != machineVO){
                dao.insert(machineVO);
                resultVO.setMessage(ExceptionConstant.insert);
            }
        }
        catch (Exception e){
            resultVO.setCode(MachineExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badInsert + e.getMessage());
        }
        return resultVO;
    }

    /**
     * 删除
     * @param param
     * @return
     */
    public ResultVO delete(JSONObject param,ResultVO resultVO){
        try {
            resultVO.setCode(MachineExceptionConstant.two);
            String id = param.getString(Constant.id);
            dao.delete(id);
            resultVO.setMessage(ExceptionConstant.delete);
        }
        catch (Exception e){
            resultVO.setCode(MachineExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badDelete + e.getMessage());
        }
        return resultVO;
    }

    /**
     * 修改
     * @param param
     * @param resultVO
     */
    public ResultVO update(JSONObject param,ResultVO resultVO){
        try{
            resultVO.setCode(MachineExceptionConstant.two);
            MachineVO machineVO = service.transJSONObjectByUpdate(param);
            if (null != machineVO){
                dao.update(machineVO);
                resultVO.setMessage(ExceptionConstant.update);
            }
        }catch (Exception e){
            resultVO.setCode(MachineExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badUpdate);
        }
        return resultVO;
    }

    /**
     * 删除判空
     */
    public void judgingEmptinessById(JSONObject param){
        String id = param.getString("id");
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("未传唯一主键");
        }
    }

    /**
     * 新增和修改判空方法
     * @param param
     * @return
     */
    public void judgingEmptiness(JSONObject param){
        //标题
        String title = param.getString(MachineConstant.title);
        if (StringUtils.isEmpty(title)) {
            throw new RuntimeException(MachineExceptionConstant.MUSTTITLE);
        }
        //适配版本
        String adaptedVersion = param.getString(MachineConstant.adaptedVersion);
        if (StringUtils.isEmpty(adaptedVersion)){
            throw new RuntimeException(MachineExceptionConstant.MUSTADAPTEDVERSION);
        }
        //投稿类型
        String submissionType = param.getString(MachineConstant.submissionType);
        if (StringUtils.isEmpty(submissionType)) {
            throw new RuntimeException(MachineExceptionConstant.MUSTSUBMISSIONTYPE);
        }
    }
}
